package alogrithm.solution;

public class P9 {
	public static boolean arrayToPostOrder(int array[],int start,int end)
	{
		System.out.println(start+":"+end);
		if(array==null||start>=end)return true;
		int lastElement=array[end];
		int i=start;
		while(i<end&&array[i]<lastElement)i++;
		int j=i;
		while(j<end&&array[j]>lastElement)j++;
		if(j==end)
		{
			return arrayToPostOrder(array, start,i-1)&&arrayToPostOrder(array, i, end-1);
		}
		return false;
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int array[]={5,7,6,9,11,10,8};
		System.out.println(arrayToPostOrder(array, 0,6));
		array=new int[]{7,4,6,5};
		System.out.println(arrayToPostOrder(array, 0, 3));

	}

}
